package org.jetnuke.dao.generic;

import org.apache.log4j.Logger;

import java.sql.PreparedStatement;
import java.sql.ResultSet;

import net.jforum.JForumExecutionContext;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.MainConfig;

public class GenericMainConfigDAO implements org.jetnuke.dao.MainConfigDAO {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(GenericMainConfigDAO.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.MainConfigModel#getMainConfig()
	 */
	public MainConfig getMainConfig() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("getMainConfig() - start"); //$NON-NLS-1$
		}

		// ResultSet rs=
		// JForumExecutionContext.getConnection().createStatement().executeQuery(SystemGlobals.getSql("MainConfigModel.select"));
		// System.out.println(SystemGlobals.getSql("MainConfigModel.select"));
		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("MainConfigModel.select"));
		// "
		// BROADCAST_MSG,CENSORMODE,CENSORREPLACE,COMMENTLIMIT,COPYRIGHT,DEFAULT_THEME,DEFAULTPOPSERVER,DOWNLOAD_DIR,EMAIL_SEND,FILTER_FORWARD,
		// "+
		// "
		// FOOT1,FOOT2,FOOT3,FOOTERMSGTXT,HTTPREF,HTTPREFMAX,IMGPATH,LANGUAGE,LOCALE,MINPASS,MODERATE,MODULE,MULTILINGUAL,MY_HEADLINES,NOTIFY,
		// "+
		// "
		// NOTIFY_EMAIL,NOTIFY_FROM,NOTIFY_MESSAGE,NOTIFY_SUBJECT,NUKEURL,NUMACCOUNTS,OLDNUM,POLLCOMM,SINGLEACCOUNT,SINGLEACCOUNTNAME,SITE_LOGO,SITENAME,
		// "+
		// "
		// SLOGAN,STARTDATE,STORYHOME,TOP,ULTRAMODE,USEFLAGS,USER_NEWS,VERSION_NUM
		// from main_config");
		ResultSet rs = p.executeQuery();
		rs.next();
		MainConfig mcf = new MainConfig();
		fillMainConfigFromResultSet(mcf, rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("getMainConfig() - end"); //$NON-NLS-1$
		}
		return mcf;
	}

	/**
	 * @param mcf
	 * @param rs
	 */
	protected void fillMainConfigFromResultSet(MainConfig mcf, ResultSet rs)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("fillMainConfigFromResultSet(MainConfig, ResultSet) - start"); //$NON-NLS-1$
		}

		mcf.setADMINGRAPHIC(rs.getInt("ADMINGRAPHIC"));
		mcf.setADMINMAIL(rs.getString("ADMINMAIL"));
		mcf.setANONPOST(rs.getInt("ANONPOST"));
		mcf.setANONYMOUS(rs.getString("ANONYMOUS"));
		mcf.setARTICLECOMM(rs.getInt("ARTICLECOMM"));
		mcf.setATTACHMENTDIR(rs.getString("ATTACHMENTDIR"));
		mcf.setATTACHMENTS(rs.getInt("ATTACHMENTS"));
		mcf.setATTACHMENTS_VIEW(rs.getInt("ATTACHMENTS_VIEW"));
		mcf.setBACKEND_LANGUAGE(rs.getString("BACKEND_LANGUAGE"));
		mcf.setBACKEND_TITLE(rs.getString("BACKEND_TITLE"));
		mcf.setBANNERS(rs.getInt("BANNERS"));
		mcf.setBROADCAST_MSG(rs.getInt("BROADCAST_MSG"));
		mcf.setCENSORMODE(rs.getInt("CENSORMODE"));
		mcf.setCENSORREPLACE(rs.getString("CENSORREPLACE"));
		mcf.setCOMMENTLIMIT(rs.getInt("COMMENTLIMIT"));
		mcf.setCOPYRIGHT(rs.getString("COPYRIGHT"));
		mcf.setDEFAULT_THEME(rs.getString("DEFAULT_THEME"));
		mcf.setDEFAULTPOPSERVER(rs.getString("DEFAULTPOPSERVER"));
		mcf.setDOWNLOAD_DIR(rs.getString("DOWNLOAD_DIR"));
		mcf.setEMAIL_SEND(rs.getInt("EMAIL_SEND"));
		mcf.setFILTER_FORWARD(rs.getInt("FILTER_FORWARD"));
		mcf.setFOOT1(rs.getString("FOOT1"));
		mcf.setFOOT2(rs.getString("FOOT2"));
		mcf.setFOOT3(rs.getString("FOOT3"));
		mcf.setFOOTERMSGTXT(rs.getString("FOOTERMSGTXT"));
		mcf.setHTTPREF(rs.getInt("HTTPREF"));
		mcf.setHTTPREFMAX(rs.getInt("HTTPREFMAX"));
		// mcf.setID(rs.getInt(""));
		mcf.setIMGPATH(rs.getString("IMGPATH"));
		mcf.setLANGUAGE(rs.getString("LANGUAGE"));
		mcf.setLOCALE(rs.getString("LOCALE"));
		mcf.setMINPASS(rs.getInt("MINPASS"));
		mcf.setMODERATE(rs.getInt("MODERATE"));
		mcf.setMODULE(rs.getString("MODULE"));
		mcf.setMULTILINGUAL(rs.getInt("MULTILINGUAL"));
		mcf.setMY_HEADLINES(rs.getInt("MY_HEADLINES"));
		mcf.setNOTIFY(rs.getInt("NOTIFY"));
		mcf.setNOTIFY_EMAIL(rs.getString("NOTIFY_EMAIL"));
		mcf.setNOTIFY_FROM(rs.getString("NOTIFY_FROM"));
		mcf.setNOTIFY_MESSAGE(rs.getString("NOTIFY_MESSAGE"));
		mcf.setNOTIFY_SUBJECT(rs.getString("NOTIFY_SUBJECT"));
		mcf.setNUKEURL(rs.getString("NUKEURL"));
		mcf.setNUMACCOUNTS(rs.getInt("NUMACCOUNTS"));
		mcf.setOLDNUM(rs.getInt("OLDNUM"));
		mcf.setPOLLCOMM(rs.getInt("POLLCOMM"));
		mcf.setSINGLEACCOUNT(rs.getInt("SINGLEACCOUNT"));
		mcf.setSINGLEACCOUNTNAME(rs.getString("SINGLEACCOUNTNAME"));
		mcf.setSITE_LOGO(rs.getString("SITE_LOGO"));
		mcf.setSITENAME(rs.getString("SITENAME"));
		mcf.setSLOGAN(rs.getString("SLOGAN"));
		mcf.setSTARTDATE(rs.getString("STARTDATE"));
		mcf.setSTORYHOME(rs.getInt("STORYHOME"));
		mcf.setTOP(rs.getInt("TOP"));
		mcf.setULTRAMODE(rs.getInt("ULTRAMODE"));
		mcf.setUSEFLAGS(rs.getInt("USEFLAGS"));
		mcf.setUSER_NEWS(rs.getInt("USER_NEWS"));
		mcf.setVERSION_NUM(rs.getString("VERSION_NUM"));

		if (logger.isDebugEnabled()) {
			logger
					.debug("fillMainConfigFromResultSet(MainConfig, ResultSet) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.MainConfigModel#update(org.jetnuke.entities.MainConfig)
	 */
	public void update(MainConfig mc) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("update(MainConfig) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("MainConfigModel.update"));
		p.setInt(1, mc.getADMINGRAPHIC());
		p.setString(2, mc.getADMINMAIL());
		p.setInt(3, mc.getANONPOST());
		p.setString(4, mc.getANONYMOUS());
		p.setInt(5, mc.getARTICLECOMM());
		p.setString(6, mc.getATTACHMENTDIR());
		p.setInt(7, mc.getATTACHMENTS());
		p.setInt(8, mc.getATTACHMENTS_VIEW());
		p.setString(9, mc.getBACKEND_LANGUAGE());
		p.setString(10, mc.getBACKEND_TITLE());
		p.setInt(11, mc.getBANNERS());
		p.setInt(12, mc.getBROADCAST_MSG());
		p.setInt(13, mc.getCENSORMODE());
		p.setString(14, mc.getCENSORREPLACE());
		p.setInt(15, mc.getCOMMENTLIMIT());
		p.setString(16, mc.getCOPYRIGHT());
		p.setString(17, mc.getDEFAULT_THEME());
		p.setString(18, mc.getDEFAULTPOPSERVER());
		p.setString(19, mc.getDOWNLOAD_DIR());
		p.setInt(20, mc.getEMAIL_SEND());
		p.setInt(21, mc.getFILTER_FORWARD());
		p.setString(22, mc.getFOOT1());
		p.setString(23, mc.getFOOT2());
		p.setString(24, mc.getFOOT3());
		p.setString(25, mc.getFOOTERMSGTXT());
		p.setInt(26, mc.getHTTPREF());
		p.setInt(27, mc.getHTTPREFMAX());
		p.setString(28, mc.getIMGPATH());
		p.setString(29, mc.getLANGUAGE());
		p.setString(30, mc.getLOCALE());
		p.setInt(31, mc.getMINPASS());
		p.setInt(32, mc.getMODERATE());
		p.setString(33, mc.getMODULE());
		p.setInt(34, mc.getMULTILINGUAL());
		p.setInt(35, mc.getMY_HEADLINES());
		p.setInt(36, mc.getNOTIFY());
		p.setString(37, mc.getNOTIFY_EMAIL());
		p.setString(38, mc.getNOTIFY_FROM());
		p.setString(39, mc.getNOTIFY_MESSAGE());
		p.setString(40, mc.getNOTIFY_SUBJECT());
		p.setString(41, mc.getNUKEURL());
		p.setInt(42, mc.getNUMACCOUNTS());
		p.setInt(43, mc.getOLDNUM());
		p.setInt(44, mc.getPOLLCOMM());
		p.setInt(45, mc.getSINGLEACCOUNT());
		p.setString(46, mc.getSINGLEACCOUNTNAME());
		p.setString(47, mc.getSITE_LOGO());
		p.setString(48, mc.getSITENAME());
		p.setString(49, mc.getSLOGAN());
		p.setString(50, mc.getSTARTDATE());
		p.setInt(51, mc.getSTORYHOME());
		p.setInt(52, mc.getTOP());
		p.setInt(53, mc.getULTRAMODE());
		p.setInt(54, mc.getUSEFLAGS());
		p.setInt(55, mc.getUSER_NEWS());

		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("update(MainConfig) - end"); //$NON-NLS-1$
		}
	}

}
